package com.demo.view;

import android.view.MotionEvent;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

public class PullLoadMore {
    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private boolean hasMore = true;
    private boolean isRefresh = false;
    private boolean isLoadMore = false;
    private boolean pullRefreshEnable = true;
    private boolean pushRefreshEnable = true;
    public  PullLoadMore(RecyclerView mRecyclerView,SwipeRefreshLayout mSwipeRefreshLayout) {
        this.mRecyclerView = mRecyclerView;
        this.mSwipeRefreshLayout = mSwipeRefreshLayout;
        this.mRecyclerView.setOnTouchListener(new onTouchRecyclerView());
    }
    private OnLoadMoreListener onLoadMoreListener;

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }

    public void setPullRefreshEnable(boolean enable) {
        pullRefreshEnable = enable;
    }
    public boolean getPullRefreshEnable() {
        return pullRefreshEnable;
    }
    public void setSwipeRefreshEnable(boolean enable) {
        mSwipeRefreshLayout.setEnabled(enable);
    }

    public SwipeRefreshLayout getSwipeRefreshLayout() {
        return mSwipeRefreshLayout;
    }

    public boolean isRefresh() {
        return isRefresh;
    }
    public boolean isLoadMore() {
        return isLoadMore;
    }

    public void setIsLoadMore(boolean isLoadMore) {
        this.isLoadMore = isLoadMore;
    }


    public void setIsRefresh(boolean isRefresh) {
        this.isRefresh = isRefresh;
    }

    public boolean isHasMore() {
        return hasMore;
    }

    public void setHasMore(boolean hasMore) {
        this.hasMore = hasMore;
    }

    public boolean getPushRefreshEnable() {
        return pushRefreshEnable;
    }

    public void loadMore() {
        if (onLoadMoreListener!=null){
            onLoadMoreListener.onLoadMore();
        }
    }
    public void setLoadMoreCompleted(){
        isRefresh = false;
        isLoadMore = false;

    }

    /**
     * Solve IndexOutOfBoundsException exception
     */
    public class onTouchRecyclerView implements View.OnTouchListener {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return isRefresh || isLoadMore;
        }
    }

    public interface OnLoadMoreListener {
        void onLoadMore();
    }
}
